import sys

from FirstExp.kam import readfile, remove_place, get_conditions, NoneException
import easygui as gui

'''
这里捕获一下异常，文本中的数据如果有空格、空隙则需要处理文本信息
处理脏数据

数据中的空格，导致的Bug是因为取值的方法不对，
parameters["好瓜"] => parameters[col[columns - 2]] 则不会引起这样的问题
'''
try:
    # 读取txt文件
    df = readfile()
    txt = df.data
    # 获取列数
    columns = txt.shape[1]
    # 算好瓜与坏瓜的概率
    col = txt.columns[1:columns]

    # 好瓜、不好的瓜分别是多少
    parameters = txt[col]
    # 总体为好瓜的个数
    good_num_y = parameters["好瓜"].value_counts()["是"]
    # 总体不是好瓜的个数
    good_num_n = parameters["好瓜"].value_counts()["否"]
    # 是好瓜的概率
    p_y = parameters["好瓜"].value_counts(normalize=True)["是"]
    # 不是好瓜的概率
    p_n = parameters["好瓜"].value_counts(normalize=True)["否"]

    # 获取用户输入条件，如果用户选进行条件选择时，点击了“cancel”，或直接关闭选择框
    # 则会引起异常，给出异常原因
    try:
        conditions = get_conditions(txt)
    except NoneException as e:
        print(e)
        sys.exit(0)

    # 判断条件是否符合要求
    j = 0
    # 临时变量
    yes = 1
    no = 1
    # 使用双层遍历获取各个属性下指定类别的概率
    for i in col[: columns - 2]:
        while j < len(conditions):
            # 满足各个条件为好瓜的数量
            condition_num_y = parameters[(parameters["好瓜"] == "是") & (parameters[i] == conditions[j])].shape[0]
            # 满足各个条件不是好瓜的数量
            condition_num_n = parameters[(parameters["好瓜"] == "否") & (parameters[i] == conditions[j])].shape[0]
            # 使用累乘获取是好瓜的概率
            yes = yes * (condition_num_y / good_num_y)
            # 使用累乘获取不是好瓜的概率
            no = no * (condition_num_n / good_num_n)
            j = j + 1
            # 这里必须添加break，内层循环只需要循环一次
            break
    # 结果是好瓜的概率
    yes = yes * p_y
    # 结果不是好瓜的概率
    no = no * p_n
    yes_msg = "此瓜是好瓜的概率为:" + str(yes) + "\n此瓜不是好瓜的概率为:" + str(no) + "\n所以条件为：" + ",".join(conditions) + "是好瓜"
    no_msg = "此瓜是好瓜的概率为:" + str(yes) + "\n此瓜不是好瓜的概率为:" + str(no) + "\n所以条件为：" + ",".join(conditions) + "不是好瓜"

    if yes > no:
        gui.msgbox(msg=yes_msg, title="结果", ok_button="确认")
    else:
        gui.msgbox(msg=no_msg, title="结果", ok_button="确认")
except KeyError:
    print(remove_place(df.path))

"""
# 好瓜、不好的瓜分别是多少
parameters = txt[col]
good_num_y = parameters["好瓜"].value_counts()["是"]
good_num_n = parameters["好瓜"].value_counts()["否"]
# 好瓜的概率
p_y = parameters["好瓜"].value_counts(normalize=True)["是"]
p_n = parameters["好瓜"].value_counts(normalize=True)["否"]
# good_list_num = good.values.tolist().count()
# isGood_no = np.sum(isGood is "是")
# 色泽
color_num_y = parameters[(parameters["好瓜"] == "是") & (parameters["色泽"] == "青绿")].shape[0]
color_num_n = parameters[(parameters["好瓜"] == "否") & (parameters["色泽"] == "青绿")].shape[0]
text = parameters[(parameters["好瓜"] == "是")]
# color_ = pd.Series(color_)
# 色泽为青绿是好瓜的概率
p_color_y = color_num_y / good_num_y
# 色泽为青绿不是好瓜的概率
p_color_n = color_num_n / good_num_n
# 根蒂
root_num_y = parameters[(parameters["好瓜"] == "是") & (parameters["根蒂"] == "稍蜷")].shape[0]
root_num_n = parameters[(parameters["好瓜"] == "否") & (parameters["根蒂"] == "稍蜷")].shape[0]
# 根蒂为稍蜷是好瓜的概率
p_root_y = root_num_y / good_num_y
# 根蒂为稍蜷不是好瓜的概率
p_root_n = root_num_n / good_num_n
# 敲声
stroke_num_y = parameters[(parameters["好瓜"] == "是") & (parameters["敲声"] == "浊响")].shape[0]
stroke_num_n = parameters[(parameters["好瓜"] == "否") & (parameters["敲声"] == "浊响")].shape[0]
# 敲声为浊响是好瓜的概率
p_stroke_y = stroke_num_y / good_num_y
# 敲声为浊响不是好瓜的概率
p_stroke_n = stroke_num_n / good_num_n
# 纹理
texture_num_y = parameters[(parameters["好瓜"] == "是") & (parameters["纹理"] == "清晰")].shape[0]
texture_num_n = parameters[(parameters["好瓜"] == "否") & (parameters["纹理"] == "清晰")].shape[0]
# 纹理为清晰是好瓜的概率
p_texture_y = texture_num_y / good_num_y
# 纹理为清晰不是好瓜的概率
p_texture_n = texture_num_n / good_num_n
# 脐部
umbilical_num_y = parameters[(parameters["好瓜"] == "是") & (parameters["脐部"] == "凹陷")].shape[0]
umbilical_num_n = parameters[(parameters["好瓜"] == "否") & (parameters["脐部"] == "凹陷")].shape[0]
# 脐部为凹陷是好瓜的概率
p_umbilical_y = umbilical_num_y / good_num_y
# 脐部为凹陷不是好瓜的概率
p_umbilical_n = umbilical_num_n / good_num_n
# 触感
touch_num_y = parameters[(parameters["好瓜"] == "是") & (parameters["触感"] == "硬滑")].shape[0]
touch_num_n = parameters[(parameters["好瓜"] == "否") & (parameters["触感"] == "硬滑")].shape[0]
# 触感为硬滑是好瓜的概率
p_touch_y = touch_num_y / good_num_y
# 触感为硬化不是好瓜的概率
p_touch_n = touch_num_n / good_num_n
# 判断有如下特征的瓜是否好瓜：青绿,稍蜷,浊响,清晰,凹陷,硬滑
# yes = p_y * p_touch_y * p_umbilical_y * p_texture_y * p_stroke_y * p_color_y * p_root_y
# no = p_n * p_touch_n * p_umbilical_n * p_texture_n * p_stroke_n * p_color_n * p_root_n
# print("是好瓜的概率：{0}".format(yes))
# print("不是好瓜的概率：{0}".format(no))
# if yes > no:
#     print("结论为：青绿,稍蜷,浊响,清晰,凹陷,硬滑的瓜是好瓜")
# else:
#     print("结论为：青绿,稍蜷,浊响,清晰,凹陷,硬滑的瓜不是好瓜")
"""
